<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item class="mr" label="闸机号" prop="gateNo">
        <el-input
          v-model="queryParams.gateNo"
          placeholder="请输入"
          clearable
          style="width: 260px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="闸机位置" prop="gatePosition">
        <el-select
          v-model="queryParams.gatePosition"
          placeholder="请选择"
          clearable
          style="width: 260px"
        >
          <el-option
            v-for="{ label, value } in zoneOptions"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="通行卡号" prop="passNo">
        <el-input
          v-model="queryParams.passNo"
          placeholder="请输入"
          clearable
          style="width: 260px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          style="width: 260px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="时间" style="width: 360px">
        <el-date-picker
          v-model="dateRange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
        />
      </el-form-item>
      <el-form-item style="margin-right: 0; margin-left: 30px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['security:entrance:records:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="recordsList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column label="序号" width="80" align="center" type="index" />
      <el-table-column label="闸机号" align="center" prop="gateNo">
        <template #default="{ row }"> {{ 'No.' + row.gateNo }} </template>
      </el-table-column>
      <el-table-column label="闸机位置" align="center" prop="gatePosition">
        <template #default="{ row }">
          <dict-tag :options="zoneOptions" :value="row.gatePosition" />
        </template>
      </el-table-column>
      <el-table-column label="通行卡号" align="center" prop="passNo" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="进入时间" align="center" prop="entryTime" />
      <el-table-column label="离开时间" align="center" prop="departureTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding  fixed-width"
      >
        <template #default="scope">
          <el-button
            icon="View"
            @click="handleView(scope.row)"
            type="text"
            v-hasPermi="['security:entrance:records:detail']"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script setup name="entranceInformation">
import {
  listInfomation,
  getInfomation,
  delInfomation,
  addInfomation,
  updateInfomation
} from '@/api/security/entrance/information'

import { zoneTreeSelect } from '@/api/emergency/mats'

const { proxy } = getCurrentInstance()

/* 获取单位字典 */
const { sys_switch_status } = proxy.useDict('sys_switch_status') //出入口开关状态

const recordsList = ref([])
const open = ref(false)
const loading = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const zoneOptions = ref([])
const openView = ref(false)
const title = ref('')
const total = ref(0)
const dateRange = ref([])
const dateRange2 = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  }
})

const { queryParams, form } = toRefs(data)

/** 存放位置树结构 */
function getZoneTree() {
  return zoneTreeSelect().then((response) => {
    zoneOptions.value = response.data
  })
}

/** 查询养护内容列表 */
function getList() {
  loading.value = true
  const [startTime, endTime] = dateRange.value ?? []
  listInfomation({ ...queryParams.value, startTime, endTime }).then(
    (response) => {
      recordsList.value = response.rows
      total.value = response.total
      loading.value = false
    }
  )
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  dateRange2.value = []
  form.value = {}
  proxy.resetForm('recordsRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加值班计划'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const recordsId = row.id || ids.value
  getInfomation(recordsId).then((response) => {
    form.value = response.data
    dateRange2.value = [response.data.startTime, response.data.endTime]
    open.value = true
    title.value = '修改值班计划'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const recordsIds = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除值班计划序号为："' + recordsIds + '"的数据项?')
    .then(function () {
      return delInfomation(recordsIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'operations/personOnInfomation/export',
    {
      ...queryParams.value
    },
    `personOnInfomation_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['recordsRef'].validate((valid) => {
    if (valid) {
      let params = Object.assign({}, form.value, {
        startTime: dateRange2.value[0],
        endTime: dateRange2.value[1]
      })
      if (form.value.id != undefined) {
        updateInfomation(params).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addInfomation(params).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}

// 获取最新搜索下拉选项
getZoneTree()

// 初始化列表
getList()
</script>
<style scoped lang="scss">
.mr {
  margin-right: 30px;
}
</style>
